#!/bin/bash
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# r42.hsc src/hsc/scripts/installImages.sh 1.22.1.22 
#  
# IBM CONFIDENTIAL 
#  
# OBJECT CODE ONLY SOURCE MATERIALS 
#  
# (C) COPYRIGHT International Business Machines Corp. 2002,2004 
# All Rights Reserved 
#  
# The source code for this program is not published or otherwise 
# divested of its trade secrets, irrespective of what has been 
# deposited with the U.S. Copyright Office. 
#  
# IBM_PROLOG_END_TAG 

#-------------------------------------------------------------------------------
# Installation return codes are:
#
# 0 - no error
# 10 - usage error
# 20 - invalid/non-existent directory on HMC
# 30 - install file not found
# 40 - RPM test verify install error
# 50 - RPM install error
# 60 - 'tar' command failure
#
# Except for call rpm command to query, all calls to rpm must go
# go through dorpm, eraserpm etc..
#
# *The actual value returned will be 1x, 2x, etc, where x will be either 0 or 4
# which is based on any RPM un-install errors. Or, '0' if successful completion
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# Initialize script return code, log file name
#-------------------------------------------------------------------------------
exitRC=0
LogFile=/tmp/HmcInstall.log

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
exit_cleanup() {
    # keep the log file, but ensure a different user can overwrite it next time
    chmod 666 $LogFile

    if [ $1 -ne 0 ]; then
        exit $1$exitRC
    else
        exit 0
    fi
}

#-------------------------------------------------------------------------------
# RPM  install
#-------------------------------------------------------------------------------
dorpm() {
    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]; then
        x="$2"
        # Strip version then leading directory name
        f=`echo ${x%%-[0-9]*}`
        r=`echo ${f##*/}`
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`
        do
            if [ "$r" = "$i" ]; then
                return
            fi
        done
    fi
     
    # Log the test install output. The format for the "normal" RPM install
    # processing inside this script is 'rpm -i <file spec> --force --nodeps'
    echo "=========================================================" >> $LogFile
    echo "***** Executing rpm -vv $* *****"                          >> $LogFile
    echo "=========================================================" >> $LogFile
    rpm -vv $* >> $LogFile 2>&1
    RC=$?
    if [ $RC -ne 0 ]; then
        echo "rpm fileset, $2, installation error, $RC" >> $LogFile
    fi
    return $RC
}

#-------------------------------------------------------------------------------
# RPM erase rpm
#-------------------------------------------------------------------------------
eraserpm() {
    # If rpm is part of no update list then
    # do not do anything with it

    if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]; then
        x="$2"
        for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`
        do
            if [ "$x" = "$i" ]; then
                return
            fi
        done
    fi
    echo "=========================================================" >> $LogFile
    echo "***** Executing rpm -vv $* *****"                          >> $LogFile
    echo "=========================================================" >> $LogFile
    rpm -vv $* >> $LogFile 2>&1
    if [ $? -ne 0 ]; then
        exitRC=4
         echo "error removing rpm fileset named $2" >> $LogFile
    fi
}

#-------------------------------------------------------------------------------
# RPM test install
#-------------------------------------------------------------------------------
testrpm() {
    rpm --test  $1 $2 $3 $4
    return $?
}

#-------------------------------------------------------------------------------
# Start the product install...
#-------------------------------------------------------------------------------
cd /
image=$1

if [ "$image" = "" ]; then
    echo "Please specify directory containing installable packages"
    echo "usage: installImages  <directory>"
    exit_cleanup 1
fi

# check if directory exists
if [ ! -d $image ]; then
    echo "The directory $patchdir doesn't exist"
    echo "Please specify directory containing the installable packages."
    exit_cleanup 2
fi

VER=`hsc version | grep Version | cut -f2 -d ":" | awk '{print $1}'`
REL=`hsc version | grep Release | cut -f2 -d ":" | awk '{print $1}'`

if [ $VER != "3" -o $REL != "3.2" ]; then
    echo "Incompatible Fix. Fix not installed"
    exit_cleanup 3
fi

if [ -f /opt/hsc/data/config/NO_UPDATE_FILES ]; then
    rm -f /tmp/saved_files.tar
    cat /opt/hsc/data/config/NO_UPDATE_FILES | xargs tar -cvf /tmp/saved_files.tar
fi

PATH=$PATH:/opt/IBMJava/jre/bin:
LD_LIBRARY_PATH=/opt/hsc/lib:/opt/hsc/lib/hcmjni:/lib:/usr/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH

if [ ! -d /opt/IBMJava ]; then
    ln -s /opt/IBMJava2-141 /opt/IBMJava
fi

# Save old log file
if [ -f $LogFile ]; then
    mv -f $LogFile ${LogFile}.old
fi

echo "=========================================================" >> $LogFile
disk=`cat $image/.signature | cut -d' ' -f 4`
if [ $disk = "0" ]; then
    Update="true"
    echo "************   Performing Update operation   ************" >> $LogFile
else
    Update="false"
    echo "************   Performing Install operation  ************" >> $LogFile
fi
echo "=========================================================" >> $LogFile

# Installing RMC and CSM
echo "--- Installing RSCT packages...."
rpm -q src 1>&2 2>/dev/null
if [ $? -eq 0 ]; then
    dorpm -Uvh $image/rmc/src-*rpm --force --nodeps 2>> $LogFile
else
    dorpm -ivh $image/rmc/src-*rpm --force --nodeps 2>> $LogFile
fi

rpm -q rsct.core.utils 1>&2 2>/dev/null
if [ $? -eq 0 ]; then
    dorpm -Uvh $image/rmc/rsct.core.utils-*rpm --force --nodeps 2>> $LogFile
else
    dorpm -ivh $image/rmc/rsct.core.utils-*rpm --force --nodeps 2>> $LogFile
fi
rpm -q rsct.core 1>&2 2>/dev/null
if [ $? -eq 0 ]; then
    dorpm -Uvh $image/rmc/rsct.core-*rpm --force --nodeps 2>> $LogFile
else
    dorpm -ivh $image/rmc/rsct.core-*rpm --force --nodeps 2>> $LogFile
fi
rpm -q csm.core 1>&2 2>/dev/null
if [ $? -eq 0 ]; then
    dorpm -Uvh $image/rmc/csm.core-*rpm --force --nodeps 2>> $LogFile
else
    dorpm -ivh $image/rmc/csm.core-*rpm --force --nodeps 2>> $LogFile
fi
rpm -q csm_hmc.server 1>&2 2>/dev/null
if [ $? -eq 0 ]; then
    dorpm -Uvh $image/rmc/csm_hmc.server-*rpm --force --nodeps 2>> $LogFile
else
    dorpm -ivh $image/rmc/csm_hmc.server-*rpm --force --nodeps 2>> $LogFile
fi
rpm -q csm.server.hsc 1>&2 2>/dev/null
if [ $? -eq 0 ]; then
    dorpm -Uvh $image/rmc/csm.server.hsc-*rpm --force --nodeps 2>> $LogFile
else
    dorpm -ivh $image/rmc/csm.server.hsc-*rpm --force --nodeps 2>> $LogFile
fi
cd /

if [ -f $image/.VERSION ]; then
    if [ -f $image/.dev ]; then
        cat $image/.VERSION >> /opt/hsc/data/version
    fi
    cat $image/.VERSION > /root/.version
    cat $image/.VERSION > /home/hscroot/.version
    chmod 644 /home/hscroot/.version
    chown hscroot.HSC_Sys_Prog /home/hscroot/.version
fi


echo "==========================================================" >> $LogFile
echo "*********************** End of Log ***********************" >> $LogFile
echo "==========================================================" >> $LogFile

exit_cleanup 0
